package MyThreadPoolExecutor2;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025-03-07
 * Time: 23:52
 */
class Executor2{
    private final int capacity = 1000;
    private final ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(capacity);
    public Executor2(int n){
        for (int i = 0; i < n; i++) {
            Thread thread = new Thread(()->{
                while (true){
                    try {
                        queue.take().run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            thread.start();
        }
    }
    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }
    public int getCapacity(){
        return capacity;
    }
}

public class MyThreadPoolExecutor2 {
    public static void main(String[] args) throws InterruptedException {
        Executor2 executor2 = new Executor2(4);
        for (int i = 0; i < executor2.getCapacity(); i++) {
            //事实final
            int n = i;
            executor2.submit(()-> System.out.println("执行任务 " + n + " " + Thread.currentThread().getName()));
        }
    }
}
